﻿Public Class frmInfoMsg

    Friend Const SECTIONNAME As String = "InfoMsgHidden"

    ''' <summary>
    ''' Show information dialog that automatically sizes itself to the message. Message can be suppressed by user. Will never show if in expert mode.
    ''' </summary>
    ''' <param name="Caption">Caption for dialog box (also serves as unique key to setting)</param>
    ''' <param name="MessageTextFormat">String format for message (\n means newline).</param>
    ''' <param name="Args">Arguments to string format.</param>
    ''' <returns>True if user wants these messages to continue to be hidden</returns>
    Friend Shared Function ShowInfoMsg(ByVal Caption As String, ByVal MessageTextFormat As String, ByVal ParamArray Args() As String) As Boolean
#If Project = "WRDBLIB" Then
        If MainWRDB IsNot Nothing AndAlso MainWRDB.ExpertMode Then Exit Function
#End If
        With New frmInfoMsg(Caption, MessageTextFormat, Args)
            .StartPosition = FormStartPosition.CenterScreen
            Application.UseWaitCursor = False
            .ShowDialog()
            .Dispose()
            Return Not CBool(GetSetting(REGAPP, SECTIONNAME, Caption, False))
        End With
    End Function

    ''' <summary>
    ''' Call this to reset settings hiding all dialogs for application.
    ''' </summary>
    Friend Shared Sub ResetAllInfoMsgs()
        Try
            DeleteSetting(REGAPP, SECTIONNAME)
        Catch ex As Exception
        End Try
    End Sub

    Friend Shared Function InfoMsgHidden(Caption As String) As Boolean
        Return GetSetting(REGAPP, SECTIONNAME, Caption, False)
    End Function

    Private Sub frmInfoBox_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If chkHide.Checked Then SaveSetting(REGAPP, SECTIONNAME, Me.Text, chkHide.Checked)
    End Sub

    Private Sub frmInfoBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oright As Integer = Me.Size.Height
        If GetSetting(REGAPP, SECTIONNAME, Me.Text, False) Then Close() : Exit Sub
        Dim ht As Integer = TextRenderer.MeasureText(lblInfo.Text, lblInfo.Font, New Drawing.Size(lblInfo.Width, Integer.MaxValue), TextFormatFlags.WordBreak Or TextFormatFlags.WordEllipsis).Height
        ht = lblInfo.CreateGraphics.MeasureString(lblInfo.Text, lblInfo.Font, lblInfo.Width).Height + 5
        lblInfo.Height = ht
        Me.ClientSize = New Drawing.Size(Me.Width, ht + btnClose.Height * 3)
        Me.Location = New Point(Me.Location.X, Me.Location.Y - (ht - oright) / 2)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
        Me.Opacity = 100
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Public Sub New(ByVal Caption As String, ByVal MessageTextFormat As String, ByVal ParamArray Args() As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Text = Caption
        lblInfo.Text = StringFormat(MessageTextFormat.Replace(vbCr, vbCrLf).Replace("\n", vbCrLf), Args)
        Me.Opacity = 0
    End Sub
End Class